#include "stdafx.h"
#include "threaddata.h"
#include "webservice.h"
#include "ctrl_userinfo.h"
#include "srvmain.h"
#ifdef _DEBUG
	#undef THIS_FILE
	static char THIS_FILE[] = __FILE__;
	#undef DEBUG_NEW
	#define	DEBUG_NEW new(_NORMAL_BLOCK,__FILE__,__LINE__)
	#define new DEBUG_NEW
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////
void web_control::log_debugsql(LPSTR filename, LPCTSTR lpSql)
{
	if(__sys_printout_debug_data__)
	{
		FILE*fp=fopen(filename,"a+");
		if(fp)
		{
			_bstr_t bstrSql(lpSql);
			fwrite((LPCSTR)bstrSql,lstrlenA(bstrSql),1,fp);
			fwrite("\r\n",strlen("\r\n"),1,fp);
			fwrite("\r\n",strlen("\r\n"),1,fp);
			fwrite("\r\n",strlen("\r\n"),1,fp);
			fwrite("\r\n",strlen("\r\n"),1,fp);
			fwrite("\r\n",strlen("\r\n"),1,fp);
			fclose(fp);
		}
	}
}
void web_control::log_debugdata(LPSTR filename, LPSTR lpData)
{
	if(__sys_printout_debug_data__)
	{
		FILE*fp=fopen(filename,"a+");
		if(fp)
		{
			fwrite(lpData,strlen(lpData),1,fp);
			fclose(fp);
		}
	}
}

LPSTR MakeMd5(LPSTR strMd5,LPCVOID lpData,DWORD dwSize);
BOOL web_control::service_change_passwd(LPVOID lpRecv,long sizeRecv,long thread_id)
{
	preq_change_passwd preq=(preq_change_passwd)lpRecv;
	ans_change_passwd ans={0};

	__CHECK_STRING__(preq->newpasswd);
	__CHECK_STRING__(preq->oldpasswd);
	do
	{
		char md5[64]={0};
		MakeMd5(md5,preq->newpasswd,lstrlenA(preq->newpasswd));
		_strupr(md5);
		try
		{
			_bstr_t bstrSql(user_logon);
			if(ans.rtCode=__userInfo__.CheckPasswd((LPCSTR)bstrSql,preq->oldpasswd)) break;

			LPSTR lpSql=_srvData[thread_id].Get2(4096);
			LPTSTR sqlConn=(LPTSTR)lpSql;
			GetSqlConn(&__dbcheck__,sqlConn);

			_ConnectionPtr pConn(__uuidof(Connection));
			pConn->PutConnectionTimeout(5L);
			pConn->Open(sqlConn,L"",L"",adOpenUnspecified);
			pConn->PutCommandTimeout(180L);

			sprintf(lpSql,"update tb_user_9000 set f9000_003v='%s' where f9000_002v='%s'",md5,(LPCSTR)bstrSql);
			pConn->Execute(bstrSql=lpSql,NULL,adCmdText);
		}
		catch(...)
		{
			ans.rtCode = error_database;
		}
	}while(0);
	if(!ans.rtCode)
		__userInfo__.ReadUserInfo();
	return(post(&ans,sizeof(ans_change_passwd)));
}
